/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Controlador;

import Dao.CatalogoDAO;
import Dao.ProductoDAO;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author -ADMIN-
 */
public class GestionProductos extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        
         if(request.getParameter("producto") != null){
            
            String pedido = request.getParameter("producto");
            HttpSession sesion = request.getSession(false);
            
            if(pedido.equals("registrarCatalogo")){
                this.registrarCatalogo(request, response);
            }
            
         }else if(request.getParameter("idCatalogo") != null){
             this.eliminarCatalogo(request, response);
         }else if(request.getParameter("idProducto") != null){
             this.eliminarProducto(request, response);
         }else{
             response.sendRedirect("error404.jsp?msg=no puede ingresar");
         }
    }
    
    private void registrarCatalogo(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
         
        String nombre = request.getParameter("txtNombreCatalogo");
        String descripcion = request.getParameter("txtDescripcion");
        
        // si devuelve 1 proceso exitoso, sino si es = 0, catalogo ya existe
        int resultado = CatalogoDAO.nuevoCatalogo(nombre, descripcion);
        
        if(resultado == 0){
            response.sendRedirect("Pages/RegistroCatalogoPaso1.jsp?msg= Catalogo ya existe");
        }else{
            response.sendRedirect("Pages/administrarCatalogo.jsp?msgAlert= Nuevo catalogo registrado");
        }
        
        
    }
    
    private void eliminarCatalogo(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    
        String idCatalogo = request.getParameter("idCatalogo");
        boolean delete = false;
        delete = CatalogoDAO.eliminarCatalogo(idCatalogo);
        if(delete != false){
            response.sendRedirect("Pages/administrarCatalogo.jsp?msg= Eliminado exitosamente");
        }else{
            response.sendRedirect("Pages/administrarCatalogo.jsp?msg= Error al eliminar el Catalogo");
        }
        
    }
    
    private void eliminarProducto(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        int idp = Integer.parseInt(request.getParameter("idProducto"));
        boolean delete = false;
        delete = ProductoDAO.eliminarProducto(idp);
        if(delete == true)
            response.sendRedirect("Pages/productosAdministrador.jsp?msg= eliminado correctamente");
        else
            response.sendRedirect("Pages/productosAdministrador.jsp?msg=Error al eliminar el producto");
        
    }
    
    
    

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
